//
//  Common.swift
//  Wallpaper
//
//  Created by YEHWANG-iOS on 2024/10/25.
//

import UIKit

var topVC: UIViewController? {
    var resultVC: UIViewController?

    let window = UIApplication.shared.delegate?.window

    resultVC = _topVC(window??.rootViewController)
    while resultVC?.presentedViewController != nil {
        resultVC = _topVC(resultVC?.presentedViewController)
    }
    return resultVC
}

private func _topVC(_ vc: UIViewController?) -> UIViewController? {
    if vc is UINavigationController {
        _topVC((vc as? UINavigationController)?.topViewController)
    } else if vc is UITabBarController {
        _topVC((vc as? UITabBarController)?.selectedViewController)
    } else {
        vc
    }
}
